package com.dandelion.common.http;

import com.rockit.core.exception.UnauthorizedException;

public class HttpContext {
    private static ThreadLocal<HttpContext> current = new ThreadLocal<HttpContext>() {
        protected HttpContext initialValue() {
            return new HttpContext();
        }
    };

    private long userId;
    private String userToken;

    public static HttpContext current() {
        return current.get();
    }

    public static void destroy() {
        if (current != null) {
            current.remove();
        }
    }

    public long getRequireUserId() {
        if (userId <= 0L) {
            throw new UnauthorizedException();
        }
        return userId;
    }

    public long getUserId() {
        return userId;
    }

    public void setUserId(long userId) {
        this.userId = userId;
    }

    public String getUserToken() {
        return userToken;
    }

    public void setUserToken(String userToken) {
        this.userToken = userToken;
    }
}
